// --------------------------------------------------------------------------------------------------------------------
// <copyright file="StorageCredentialsComparer.cs" company="AzureContrib (Open source library http://azurecontrib.codeplex.com)">
//   Microsoft Public License (Ms-PL)
// </copyright>
// <summary>
//   <see cref="IEqualityComparer{T}" /> for <see cref="StorageCredentials" />
// </summary>
// --------------------------------------------------------------------------------------------------------------------

using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

using Microsoft.WindowsAzure;

namespace AzureContrib.WindowsAzure.StorageClient
{
	/// <summary>
	/// <see cref="IEqualityComparer{T}"/> for <see cref="StorageCredentials"/>
	/// </summary>
	public class StorageCredentialsComparer : IEqualityComparer<StorageCredentials>
	{
		/// <summary>
		/// Uses an <see cref="IEqualityComparer{T}.Equals(T,T)"/> to compare the credentials.
		/// </summary>
		/// <param name="x">First <see cref="StorageCredentials"/></param>
		/// <param name="y">Second <see cref="StorageCredentials"/></param>
		/// <returns>True if the <see cref="StorageCredentials"/> are equal.</returns>
		[SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification = "ObjectEqualityHelpers.NotEquatable handles null checking.", Scope = "method")]
		public bool Equals(StorageCredentials x, StorageCredentials y)
		{
			bool result;
			if (ObjectEqualityHelpers.NotEquatable(x, y, out result))
			{
				return result;
			}

			return x.AccountName == y.AccountName &&
						 x.CanComputeHmac == y.CanComputeHmac &&
						 x.CanSignRequest == y.CanSignRequest &&
						 x.CanSignRequestLite == y.CanSignRequestLite &&
						 x.NeedsTransformUri == y.NeedsTransformUri;
		}

		/// <summary>
		/// Calls <see cref="IEqualityComparer{T}.GetHashCode(T)"/>
		/// </summary>
		/// <param name="obj">The <see cref="StorageCredentials"/> to calculate hash for.</param>
		/// <returns>The hash code for <see cref="StorageCredentials"/></returns>
		public int GetHashCode(StorageCredentials obj)
		{
			if (obj == null)
// ReSharper disable HeuristicUnreachableCode
			{
				return 0;
			}
// ReSharper restore HeuristicUnreachableCode
			return obj.AccountName.GetHashCode() ^
						 obj.CanComputeHmac.GetHashCode() ^
						 obj.CanSignRequest.GetHashCode() ^
						 obj.CanSignRequestLite.GetHashCode() ^
						 obj.NeedsTransformUri.GetHashCode();
		}
	}
}